//
//  HMUserAccountViewModel.m
//  SinaWeiBo
//
//  Created by WGP on 16/4/11.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "HMUserAccountViewModel.h"
#import "HMNetWorkTool.h"
#import "HMUserAccount.h"
#import "HMNetWorkTool.h"

@implementation HMUserAccountViewModel

+(instancetype)sharedInstance
{
    static HMUserAccountViewModel *instance  = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        instance = [[HMUserAccountViewModel alloc] init];
    });
    return instance;
}

-(instancetype)init
{
    if(self = [super init])
    {

        [self loadUserAccount];
       
    }
    return self;
}

/**
 *  加载AccessToken
 *
 *  @param code     code
 *  @param finished 完成回调
 */
-(void)loadAccessToken:(NSString *)code loadAccessTokenFinished:(void (^)(NSError *error))finished
{
    NSString *atURL = accessTokenURL;
    
    NSDictionary *parameters =
    @{
        @"client_id":client_id,
        @"client_secret":client_secret,
        @"grant_type":grant_type,
        @"code":code,
        @"redirect_uri":redirect_uri
    };
    
    [[HMNetWorkTool sharedNetWorkTool] POST:atURL parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        HMLog(@"%@",responseObject);
        
        /*
         {
         "access_token" = "2.00oqxoiD2thVQC404cc9e38dAOk2SE";
         "expires_in" = 157679999;
         "remind_in" = 157679999;
         uid = 3410703750;
         }
         */
        NSDictionary *result = (NSDictionary *)responseObject;
        
        self.userAccount = [HMUserAccount userAccountWithDict:result];
        
        [self saveUserAccount:self.userAccount];
        
        finished(nil);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        HMLog(@"%@",error);
        
        finished(error);
    }];
}

/**
 *  家在用户信息
 */
-(void)loadUserInfo:(void(^)(NSError *error))loadUserInfoFinished
{
//    1.URL  https://api.weibo.com/2/users/show.json?access_token=2.00oqxoiD2thVQC9f182a5e6ePCNCEE
    NSString *userURL = @"https://api.weibo.com/2/users/show.json";
    
//    2.参数

    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"access_token"] = self.userAccount.access_token;
    
    dict[@"uid"] = self.userAccount.uid;
   
    
//    3.get请求
    [[HMNetWorkTool sharedNetWorkTool] GET:userURL parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
//        给账户模型赋值
        NSDictionary *result = (NSDictionary *)responseObject;
        self.userAccount.screen_name = result[@"screen_name"];
        self.userAccount.avatar_large = result[@"avatar_large"];
        
//        保存下来
        [self saveUserAccount:self.userAccount];
        

        
        loadUserInfoFinished(nil);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        loadUserInfoFinished(error);
    }];
}

/**
 *  用户是否登陆
 *
 *  @return Bool
 */
-(BOOL)userLogin
{
    if(_userAccount != nil)
    {
        return YES;
    }
    return NO;
}

#pragma mark - 私有的归档解档方法

-(void)saveUserAccount:(HMUserAccount *)userAccount
{
    
    [NSKeyedArchiver archiveRootObject:userAccount toFile:filepath];
//    HMLog(@"%@",filepath);
    
    
}

-(void)loadUserAccount
{
   _userAccount = [NSKeyedUnarchiver unarchiveObjectWithFile:filepath];
}


@end











